package com.hyyt.shopjsp.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicArea;
import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.basic.service.imp.BasicAreaService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.customer.pojo.ShopCustomerAcceptAddress;
import com.hyyt.shopjsp.customer.service.IShopCustomerAcceptAddressService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/4/5 0024下午 14:50.
 */
@Controller
@RequestMapping("/back/customerAcceptAddress")
public class ShopCustomerAcceptAddressController extends BaseController {
	private Logger log = Logger.getLogger(ShopCustomerAcceptAddressController.class);
	private String basePath = "/back/shop/customerAcceptAddress/";

	@Resource
	private IShopCustomerAcceptAddressService shopCustomerAcceptAddressService;//会员收货地址service
	@Resource
	private BasicAreaService basicAreaService;//地区表service
	/**
	 * 进入会员收货地址管理
	 * @param customerId
	 * @param purviewId
	 * @param param
	 * @return
	 */
	@RequestMapping("findAcceptAddressListByCustomerId")
	public String findAcceptAddressListByCustomerId(String customerId,Map<String, Object> param){
        param.put("customerId", customerId);
		return basePath+"customerAcceptAddressList";
	}


	/**
	 * 根据用户ID得到用户地址List
	 * @param customerId
	 * @param consignee
	 * @param isSendAddress
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listCustomerAcceptAddress",method=RequestMethod.POST)
	@ResponseBody
	public void listCustomerAcceptAddress(Integer customerId,String consignee,String isSendAddress,Page page, HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(consignee)){//是否检索邮箱
			param.put("consignee", consignee.trim());
		}
		if(Utils.objectIsNotEmpty(isSendAddress)){//是否检索状态
			param.put("isSendAddress", isSendAddress);
		}
		param.put("customerId", customerId);
		param.put("sortColumns", "customerAcceptAddressId desc");
		int totalRecordCount =shopCustomerAcceptAddressService.countShopCustomerAcceptAddress(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopCustomerAcceptAddress> customerAcceptAddressList =shopCustomerAcceptAddressService.queryShopCustomerAcceptAddress(param, page.getPageRecordBeginIndex(), page.getPageSize());
		if(customerAcceptAddressList!=null&&customerAcceptAddressList.size()>0){
			for (ShopCustomerAcceptAddress ca : customerAcceptAddressList) {
				if(Utils.stringIsNotEmpty(ca.getAreaCounty())){
					BasicArea areaCounty = basicAreaService.loadBasicArea(Integer.parseInt(ca.getAreaCounty()));
					ca.setAreaCounty(areaCounty.getName());
				}else{
					ca.setAreaCounty("-");
				}if(Utils.stringIsNotEmpty(ca.getCity())){
					BasicArea city=basicAreaService.loadBasicArea(Integer.parseInt(ca.getCity()));
					ca.setCity(city.getName());
				}else{
					ca.setCity("-");
				}
				if(Utils.stringIsNotEmpty(ca.getRegionLocation())){
					BasicArea regionLocation=basicAreaService.loadBasicArea(Integer.parseInt(ca.getRegionLocation()));
					ca.setRegionLocation(regionLocation.getName());
				}else{
					ca.setRegionLocation("-");
				}
			}
		}
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", customerAcceptAddressList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 获得金币明细对象
	 * @param customerAcceptAddressId
	 * @return
	 */
	@RequestMapping(value="getCustomerAcceptAddressObject",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getCustomerAcceptAddressObject(Integer customerAcceptAddressId){
		if(Utils.objectIsNotEmpty(customerAcceptAddressId)){
			ShopCustomerAcceptAddress shopCustomerAcceptAddress = shopCustomerAcceptAddressService.loadShopCustomerAcceptAddress(customerAcceptAddressId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopCustomerAcceptAddress));
		}else {
			return ResultMsg.create(false);
		}
	}

}